package tezhongbing.socket;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class SocketServer {
	public static void main(String[] args) throws IOException {
		ServerSocket serverSocket = null;
		Socket socket = null;
		try {
			serverSocket = new ServerSocket(8888);
			System.out.println("端口8888已经打开，准备接受数据！");
			socket = serverSocket.accept();
			BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
			BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
			String input = null;
			while(!"bye".equalsIgnoreCase(input=reader.readLine())){
				System.out.println("客户端传来数据："+input);
				writer.write("我接收到你的数据："+input);
				writer.newLine();
				writer.flush();
			}
			writer.write("close");
			writer.newLine();
			writer.flush();
		}finally{
			if(serverSocket != null){
				serverSocket.close();
			}
		}
	}
}
